package com.yc.wxcontroller;

import com.yc.entity.Attendance;
import com.yc.entity.Schedule;
import com.yc.exception.BizException;
import com.yc.mapper.ScheduleMapper;
import com.yc.service.AttendanceRecordsService;
import com.yc.service.AttendanceService;
import com.yc.service.ScheduleService;
import com.yc.vo.AttendanceData;
import com.yc.vo.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author zhupanlin
 * @version 1.0
 * @description: TODO
 * @date 2023/7/24 11:04
 */
@Api("微信管理考勤控制器")
//@RestController
//@RequestMapping("/wx/attendance")
public class WXAttendanceController {
    @Resource
    AttendanceService attendanceService;

    @Resource
    AttendanceRecordsService attendanceRecordsService;

    @Resource
    ScheduleService scheduleService;

    @Resource
    ScheduleMapper scheduleMapper;

    @ApiOperation("查询当前老师上课班级的学生")
    @GetMapping("/get/{username}")
    public Result selectStudentBySchedule(@ApiParam(name = "username", value = "上课老师的工号", required = true)
                                          @PathVariable("username") String username){
        return Result.success(attendanceService.selectStudentByAttId(username));
    }

    @ApiOperation("手动添加考勤信息")
    @PostMapping("/add/{username}")
    public Result addAttendance(@ApiParam(name = "username", value = "上课老师的工号", required = true)
                                @PathVariable("username") String username,
                                @RequestParam("classId") String classId, @RequestParam("arrivecount") Integer arrivecount){
        Attendance attendance = new Attendance();
        attendance.setClassId(classId);
        attendance.setArrivedcount(arrivecount);
        attendance.setCreateBy(username);
        attendance.setUpdateBy(username);
        attendance.setSignmethod(4);
        Schedule schedule = scheduleService.selectScheduleByCidAndDate(username, classId);
        attendance.setScheduleid(schedule.getId());
        // 插入之前查询是否有记录
        Integer attId = attendanceService.selectAttendanceByScheduleId(schedule.getId() + "");
        attendance.setId(attId);
        Result result;
        // 有记录 则 修改 没有 就新增
        if (attId == null || attId <= 0){
            attendanceService.addAttendance(attendance);
            result = new Result(1, attendance.getId());
            result.setMsg("添加成功!");
        }else {
            // 修改
            attendanceService.modifyAttendance(attendance);
            result = new Result(1, attId);
            result.setMsg("添加成功!");
        }
        return result;
    }


    @ApiOperation("查询学生有考勤的年份")
    @GetMapping("/get/allyears/{stuid}")
    public Result selectAllYears(@ApiParam(name = "stuid", value = "学生学号", required = true)
                                 @PathVariable("stuid") String stuid){
        List<String> years = attendanceService.selectAllYears(stuid);
        return Result.success(years);
    }

    @ApiOperation("查询学生有考勤的月份")
    @GetMapping("/get/allmonths/{beginDate}")
    public Result selectAllMonths(@ApiParam(name = "beginDate", value = "考勤的年份", required = true)
                                 @PathVariable("beginDate") Integer beginDate){
        List<String> dates = attendanceRecordsService.selectNowSchoolYear(beginDate);
        List<String> months = attendanceService.selectAllMonths(dates.get(0), dates.get(1));
        //List<String> months = attendanceService.selectAllMonths("2023-07-01", "2024-07-01");
        return Result.success(months);
    }

    @ApiOperation("老师发布快捷考勤")
    @PostMapping("/quickDeploy")
    public Result quickDeploy(@RequestBody List<AttendanceData> attendancelist) throws BizException {
        final Integer integer = attendanceService.addQuick(attendancelist);
        if (integer>0){
            return Result.success("发布成功");
        }
        return Result.error("发布失败");
    }

    @ApiOperation("捕获Biz异常")
    @ExceptionHandler(BizException.class)
    public Result handelBizException(BizException e){
        return Result.error(e.getMessage());
    }
}
